﻿//-----------------------------------------------------------------
//
// @(#)$Id: PayPalAccountDetail.aspx.cs,v 1.0 2013/12/13 17:21:27 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 17:21:27 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using com.paypal.soap.api;
using ebay.Dal.CenterDB_EbayTableAdapters;

public partial class UI_Bas_PayPalAccountDetail : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e); 
        string[] status = Enum.GetNames(typeof(PayPalUserStatusCodeType));
        foreach (string stat in status)
        {
            if (stat != "CustomCode")
            {
                ddlAccountType.Items.Add(stat);
            }
        }        
    }

    static string act = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {     
            act = Request.QueryString["act"];
            if (!string.IsNullOrEmpty(act))
            {
                switch (act)
                {
                    case "I":
                        break;
                    case "U":                       
                        txtPayPalUserId.Attributes["readonly"] = "readonly";                         
                        string PaypalId = Request.QueryString["id"];
                        T_PayPalAccountTableAdapter paypalAdapter = new T_PayPalAccountTableAdapter();
                        DataTable table = paypalAdapter.GetDataByPaypalId(PaypalId);                      
                        if (table.Rows.Count > 0)
                        {
                            DataRow r = table.Rows[0];
                            foreach (Control txtControl in this.form1.Controls)
                            {
                                if (txtControl is HtmlInputText)
                                {
                                    HtmlInputText txtBox = (HtmlInputText)txtControl;
                                    string colName = txtBox.ID.Remove(0, 3);
                                    string val = r[colName].ToString();
                                    if (table.Columns[colName].DataType.Name == "DateTime")
                                    {
                                        DateTime valDate = DateTime.Parse(val);
                                        DateTime defaultDateTime = new DateTime(1900, 1, 1);
                                        if (valDate == defaultDateTime)
                                        {
                                            val = "";
                                        }
                                    }
                                    txtBox.Value = val;
                                }
                                else if (txtControl is HtmlSelect)
                                {
                                    HtmlSelect ddList = (HtmlSelect)txtControl;
                                    string colName = ddList.ID.Remove(0, 3);
                                    ddList.Value = r[colName].ToString();

                                }
                            }
                        }
                        break;                     
                }
            }
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)
        {
            WebUtility.ShowMsg("登陆超时,请重新登陆", this);
            return;
        }
        string userId = Session["UserId"].ToString();

        if(!string .IsNullOrEmpty (act))
        {
            switch (act)
            {
                case "I":
                    {
                        T_PayPalAccountTableAdapter paypalAdapter = new T_PayPalAccountTableAdapter();
                        int flag = (int)paypalAdapter.IsExists(txtPayPalUserId.Value);
                        if (flag == 0)
                        {
                            int val = paypalAdapter.Insert(
                                     txtPayPalUserId.Value, ddlAccountType.Value, txtAPIUsername.Value, txtAPIPassword.Value, txtAPISignature.Value,
                                     txtAccountManagerName.Value, txtAccountManagerPhone.Value, txtAccountManagerEmail.Value, txtDefaultEmailAddress.Value,
                                     txtOtherEmailAddress1.Value, txtOtherEmailAddress2.Value, txtOtherEmailAddress3.Value, txtOtherEmailAddress4.Value,
                                     txtOtherEmailAddress5.Value, txtOtherEmailAddress6.Value, txtOtherEmailAddress7.Value, txtRegisterCity.Value,
                                     txtDefaultRegisterPhone.Value, txtRegisterPhone1.Value, txtRegisterPhone2.Value, txtRegisterPhone3.Value, txtRegisterPhone4.Value,
                                     txtRegisterPhone5.Value, txtRegisterPhone6.Value, txtRegisterPhone7.Value, txtDefaultRegisterAddresser.Value, txtRegisterAddresser1.Value,
                                     txtRegisterAddresser2.Value, txtRegisterAddresser3.Value, txtRegisterAddresser4.Value, txtRegisterAddresser5.Value, txtRegisterAddresser6.Value,
                                     txtRegisterAddresser7.Value, txtRegisterName.Value, txtDefaultBackAccount.Value, txtBackAccount1.Value, txtBackAccount2.Value, txtBackAccount3.Value,
                                     txtBackAccount4.Value, txtBackAccount5.Value, txtBackAccount6.Value, txtBackAccount7.Value, txtDefaultCreditCard.Value, txtCreditCard1.Value,
                                     txtCreditCard2.Value, txtCreditCard3.Value, txtCreditCard4.Value, txtCreditCard5.Value, txtCreditCard6.Value, txtCreditCard7.Value,
                                     txtPPAccountManager.Value, txtIPAddresser.Value, txtIPAddresserCompany.Value, txtMnote.Value,
                                     userId,
                                     DateTime.Now,
                                     "",
                                     new DateTime(1900, 1, 1));
                            if (val > 0)
                            {
                                Response.Write("<script>alert('添加成功!');window.location.href='PayPalAccount.aspx'</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('此编号已经存在!..');</script>");
                        }
                    }
                    break;
                case "U":
                    {
                        string PaypalId = Request.QueryString["id"];
                        T_PayPalAccountTableAdapter paypalAdapter = new T_PayPalAccountTableAdapter();
                        DataTable dt = paypalAdapter.GetDataByPaypalId(PaypalId);
                        if (dt.Rows.Count > 0)
                        {
                            DataRow r = dt.Rows[0];
                            foreach (Control txtControl in this.form1.Controls)
                            {
                                if (txtControl is HtmlInputText)
                                {
                                    HtmlInputText txtBox = (HtmlInputText)txtControl;
                                    string colName = txtBox.ID.Substring(3, txtBox.ID.Length - 3);
                                    if (txtBox.Value != r[colName].ToString())
                                    {
                                        r[colName] = txtBox.Value;
                                    }
                                }
                                else if (txtControl is HtmlSelect)
                                {
                                    HtmlSelect ddList = (HtmlSelect)txtControl;
                                    string colName = ddList.ID.Substring(3, ddList.ID.Length - 3);
                                    if (ddList.Value != r[colName].ToString())
                                    {
                                        r[colName] = ddList.SelectedIndex;
                                    }
                                }
                            }
                            r["Muser"] = userId;
                            r["Mtime"] = DateTime.Now;
                            r["Auser"] = "";
                            r["Atime"] = DateTime.Now;
                            int val = paypalAdapter.Update(dt.Rows[0]);
                            if (val > 0)
                            {
                                Response.Write("<script>alert('修改成功!');window.location.href='PayPalAccount.aspx'</script>");
                            }
                        }
                    }
                    break;
                default :
                    break;
            }
        }
    }
}
